clear matrix
clear mata
clear

set maxvar 8000


capture cd  "/Users/Oliver/Dropbox/Shared/Sent-Down/"


*******************************************
****Merge datasets and rename variables****
*******************************************

use "CFPS2012 in STATA (English)/ecfps2012adult_201906.dta", clear
foreach var of varlist _all{
rename `var' `var'_2012
}
rename pid_2012 pid
save "2012.dta", replace

use "CFPS2014 in STATA (English)/ecfps2014adult_201906.dta"
foreach var of varlist _all{
rename `var' `var'_2014
}
rename pid_2014 pid
save "2014.dta", replace

use "CFPS2016 in STATA (English)/ecfps2016adult_201906.dta"
foreach var of varlist _all{
rename `var' `var'_2016
}
rename pid_2016 pid
save "2016.dta", replace

use "CFPS2010 in STATA (English)/ecfps2010adult_201906.dta", clear

merge 1:1 pid using "2012.dta"
gen merge=""
replace merge="2010 2012" if _merge==3
replace merge="2012" if _merge==2
replace merge="2010" if _merge==1
drop _merge

merge 1:1 pid using "2014.dta"
replace merge="2010 2012 2014" if _merge==3 & merge=="2010 2012"
replace merge="2014" if _merge==2
replace merge="2010 2014" if _merge==3 & merge=="2010"
replace merge="2012 2014" if _merge==3 & merge=="2012"
drop _merge

merge 1:1 pid using "2016.dta"
replace merge="2010 2012 2014 2016" if _merge==3 & merge=="2010 2012 2014"
replace merge="2016" if _merge==2
replace merge="2010 2014 2016" if _merge==3 & merge=="2010 2014"
replace merge="2010 2012 2016" if _merge==3 & merge=="2010 2012"
replace merge="2012 2014 2016" if _merge==3 & merge=="2012 "
replace merge="2010 2016" if _merge==3 & merge=="2010"
replace merge="2012 2016" if _merge==3 & merge=="2012"
replace merge="2014 2016" if _merge==3 & merge=="2014"
drop _merge


*cleaning
    gen birthyear=qa1y

    recode  qm*  qn*  qa402 qa401acode  cfps_party_2014 (-1=.)(-2=.)(-8=.)(-9=.)(77=.)(78=.)(79=.)

    rename qn1101_2016 local_gov_achievement_2016
    rename qn1101_2014 local_gov_achievement_2014
    rename qn1101_2012 local_gov_achievement_2012
    rename qn4 local_gov_achievement_2010

    rename qn6011_2012 problem_corruption_2012
    rename qn6012_2012 problem_environment_2012
    rename qn6013_2012 problem_inequality_2012
    rename qn6014_2012 problem_employment_2012
    rename qn6015_2012 problem_education_2012
    rename qn6016_2012 problem_medical_2012
    rename qn6017_2012 problem_housing_2012
    rename qn6018_2012 problem_socialsecurity_2012

    rename qn6011_2014 problem_corruption_2014
    rename qn6012_2014 problem_environment_2014
    rename qn6013_2014 problem_inequality_2014
    rename qn6014_2014 problem_employment_2014
    rename qn6015_2014 problem_education_2014
    rename qn6016_2014 problem_medical_2014
    rename qn6017_2014 problem_housing_2014
    rename qn6018_2014 problem_socialsecurity_2014

    rename qn6011_2016 problem_corruption_2016
    rename qn6012_2016 problem_environment_2016
    rename qn6013_2016 problem_inequality_2016
    rename qn6014_2016 problem_employment_2016
    rename qn6015_2016 problem_education_2016
    rename qn6016_2016 problem_medical_2016
    rename qn6017_2016 problem_housing_2016
    rename qn6018_2016 problem_socialsecurity_2016

    rename qn1001_2012 general_trust_2012
    rename qn10025_2012 cadre_trust_2012
    rename qn1001_2014 general_trust_2014
    rename pn1001_2016 general_trust_2016
    rename qn10025_2014 cadre_trust_2014
    rename qn10025_2016 cadre_trust_2016
    rename qn10023_2012 american_trust_2012
    rename qn10023_2014 american_trust_2014
    rename qn10023_2016 american_trust_2016
    rename qn10021_2012 parent_trust_2012
    rename qn10021_2014 parent_trust_2014
    rename qn10021_2016 parent_trust_2016
    rename qn10022_2012 neighbor_trust_2012
    rename qn10022_2014 neighbor_trust_2014
    rename qn10022_2016 neighbor_trust_2016
    rename qn10026_2016 doctor_trust_2016
    rename qn10026_2014 doctor_trust_2014
    rename qn10026_2012 doctor_trust_2012
    rename qn10024_2016 stranger_trust_2016
    rename qn10024_2014 stranger_trust_2014
    rename qn10024_2012 stranger_trust_2012

    rename qn1014_2012 unfair_treat_2012
    rename qn1014_2014 unfair_treat_2014
    rename qn1014_2016 unfair_treat_2016
    rename qn1015_2012 conflict_2012
    rename qn1015_2014 conflict_2014
    rename qn1015_2016 conflict_2016
    rename qn1016_2012 delay_2012
    rename qn1016_2014 delay_2014
    rename qn1016_2016 delay_2016
    rename qn207 delay_2010
    rename qn206 conflict_2010
    rename qn205 unfair_treat_2010

*averaged variables
    recode local_gov_achievement* (1=5) (2=4) (4=2) (5=1)
    egen contructed_local_gov_achievement=rmean(local_gov_achievement*) 
    egen contructed_problem_corruption=rmean(problem_cor*)  
    egen total_problems_2016 =rmean(problem_environment_2016 problem_inequality_2016 problem_employment_2016 problem_education_2016 problem_medical_2016 problem_housing_2016 problem_socialsecurity_2016)
    egen total_problems_2014 =rmean(problem_environment_2014 problem_inequality_2014 problem_employment_2014 problem_education_2014 problem_medical_2014 problem_housing_2014 problem_socialsecurity_2014)
    egen total_problems_2012 =rmean(problem_environment_2012 problem_inequality_2012 problem_employment_2012 problem_education_2012 problem_medical_2012 problem_housing_2012 problem_socialsecurity_2012)
    egen contructed_total_problems=rmean(total_problems_2016 total_problems_2014 total_problems_2012)
    recode conflict* (5=0)
    egen constructed_conflict=rmean(conflict_2010 conflict_2012 conflict_2014 conflict_2016)

    egen contructed_parent_trust=rmean(parent_trust*)
    egen contructed_cadre_trust=rmean(cadre_trust*)
    egen contructed_stranger_trust=rmean(stranger_trust*)

*sent down
    gen sent_down=0
        replace sent_down=1 if qg103acode_a_1>0
        replace sent_down=. if qg103acode_a_1==.
    gen lateleaving=0
        replace lateleaving=1 if  qg102_a_1>1980 & qg102_a_1<1990
    replace qc602=. if qc602<0
*rdmodel
    gen rdsample=0
        replace rdsample=1 if birthyear>=1949 & birthyear<1973
    *urban residents only
        replace rdsample=0 if qa402!=3
*pretreatment variables
    gen class_background=qa6
        recode class_background (-8=.)(-1=.)(1=1)(2=1)(3=1)(4=1)(5=1)(6=1)(7=1)(8=1)(9=1)(10=2)(11=2)(12=2)(13=2)(14=2)(15=2)(16=2)(17=2)(18=2)(20=1)(21=1)(22=1)(24=2)(25=2)(26=2)(28=2)(77=1)
        quietly tab class_background, gen(cr_)
    gen minority = 0 if qa5code==1
        replace minority =1 if qa5code>1
*vote
    gen vote=.
        replace vote=0 if qn7_2014==5
        replace vote=1 if qn7_2014==1

    gen vote_forced=.
        replace vote_forced=1 if qn701==5 
        replace vote_forced=0 if qn701==1
*party membership
    gen party_member_2010=.
        replace party_member_2010=1 if qa701>0 & qa701 != .
        replace party_member_2010=0 if qa701==-8

*education and income
    gen edudegree=cfps2010edu_best
        replace edudegree=edu2012_2012 if edudegree==.
        replace edudegree=cfps2014edu_2014 if edudegree==.
        replace edudegree=cfps2016edu_2016 if edudegree==.
        recode edudegree (-1=.)(-9=.)
    
    gen logincome=log(income)

    gen high_edudegree=.
        replace high_edudegree=1 if edudegree>3&edudegree!=.
    gen high_income=.
        replace high_income=1 if logincome>9.68&logincome!=.

**concern and reliability from interviewer
    recode qz210 qz210_2014 qz210_2016 qz210_2012 (-8=.) (-1=.) (-9=.)
        egen concern=rmean(qz210_2014 qz210_2016 qz210_2012 qz210)
 
    recode qz211 qz211_2014 qz211_2016 qz211_2012 (-8=.) (-1=.) (-9=.)
        egen reliability=rmean(qz211_2014 qz211_2016 qz211_2012 qz211)

*no response
    gen noresponse_corruption=0
        replace noresponse_corruption=1 if contructed_problem_corruption==.
    gen noresponse_problems=0
        replace noresponse_problems=1 if contructed_total_problems==.
    gen noresponse_localgov=0
        replace noresponse_localgov=1 if contructed_local_gov_achievement==.
    gen noresponse_vote=0
        replace noresponse_vote=1 if vote==.
    gen noresponse_membership=0 
        replace noresponse_membership=1 if party_member_2010==.
    gen noresponse_conflict=0
        replace noresponse_conflict=1 if constructed_conflict==.
    gen noresponse_strangers=0
        replace noresponse_strangers=1 if contructed_stranger_trust==.
    gen noresponse_cadres=0
        replace noresponse_cadres=1 if contructed_cadre_trust==.
    gen noresponse_parents=0
        replace noresponse_parents=1 if contructed_parent_trust==.

*confidence and happiness
    egen future_confidence=rmean(qm404 qn12014_2012 qn12014_2014 qn12014_2016)
        replace qk802 = . if qk802 < 0
        gen happiness_2014=qm2012_2014/2
        gen happiness_2016=qm2014_2016/2
    egen happiness=rmean(qk802 happiness_2014 happiness_2016)

*placebo outcomes
        recode qm6_2014 (5=0)
        recode pm6_2016 (-1=.) (5=0)
    egen altruism = rmean(qm6_2014 pm6_2016)
    egen social_status = rmean(qm402 qn12011_*)
    egen satisfaction_life = rmean(qm403 qn12012_2012 qn12012_2014 qn12012_2016)
    gen ever_smoke = qq211
        replace ever_smoke =. if qq211 < 0
    recode delay_* (3=0)(5=0)
    egen constructed_delay=rmean(delay_*)

*binary scale
    gen corruption_binary=.
        replace corruption_binary=1 if contructed_problem_corruption>7.5&contructed_problem_corruption<=10
        replace corruption_binary=0 if contructed_problem_corruption>=0&contructed_problem_corruption<=7.5
    gen problems_binary=.
        replace problems_binary=1 if contructed_total_problems>6.8&contructed_total_problems<=10
        replace problems_binary=0 if contructed_total_problems>=0&contructed_total_problems<=6.8
    gen localgov_binary=.
        replace localgov_binary=1 if contructed_local_gov_achievement>3.6&contructed_local_gov_achievement<=5
        replace localgov_binary=0 if contructed_local_gov_achievement>=1&contructed_local_gov_achievement<=3.6
    gen stranger_trust_binary=.
        replace stranger_trust_binary=1 if contructed_stranger_trust>1.7&contructed_stranger_trust<=10
        replace stranger_trust_binary=0 if contructed_stranger_trust>=0&contructed_stranger_trust<=1.7
    gen cadre_trust_binary=.
        replace cadre_trust_binary=1 if contructed_cadre_trust>4.3&contructed_cadre_trust<=10
        replace cadre_trust_binary=0 if contructed_cadre_trust>=0&contructed_cadre_trust<=4.3
    gen parent_trust_binary=.
        replace parent_trust_binary=1 if contructed_parent_trust==10
        replace parent_trust_binary=0 if contructed_parent_trust>=0&contructed_parent_trust<10

save "merged_cfps_replication.dta", replace
